<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * redis class
 * Created by PhpStorm.
 *
 * User: Maqiong
 * Date: 14-7-4
 * Time: 上午10:08
 */
class CT_Redis extends Redis {

	/**
	 * CI
	 *
	 * CodeIgniter instance
	 * @var 	object
	 */
	private $_ci;

    //private $_redis;

	/**
	 * Constructor
	 */
	function __construct()
	{
		log_message('debug', 'Redis Class Initialized');
		$this->_ci = get_instance();
        $this->_ci->config->load('redis', TRUE);
        $config_redis = $this->_ci->config->item('redis');


        //$this->_redis = new Redis();
        $conn = $this->connect($config_redis['redis_host'], $config_redis['redis_port']);

        if ( ! $conn )
        {
            show_error('Could not connect to Redis at ' . $config_redis['redis_host'] . ':' . $config_redis['redis_port']);
        }

	}

    /**
     * 缓存名称处理
     * @param $prefix_mod  模块前缀
     * @param $key         设置key
     * @return string
     */
    private function cache_name_handle($prefix_mod, $key)
    {
        $prefix = 'prefix_ryb2016';
        return md5(trim($prefix . $prefix_mod . $key));
    }

    /**
     * 缓存存储值处理
     * @param   $data   数组和对象为json格式 字符串和数字,直接存
     * @return string
     */
    private function cache_save_value($data)
    {

        if(is_array($data) || is_object($data))
        {
            $data = json_encode($data);
        }

        return $data;
    }

    /**
     * 获取缓存信息
     * @return string
     */
    public function get_cache_info()
    {
        return $this->info();
    }

    public function close_cache()
    {
        return $this->close();
    }

    /**
     * 获取value
     * @param $prefix_mod
     * @param $key
     * @return bool|string
     */
    public function ct_get($prefix_mod, $key)
    {
        $key = $this->cache_name_handle($prefix_mod, $key);

        return $this->get($key);
    }

    /**
     * 设置value
     * @param $prefix_mod  模块前缀
     * @param $key
     * @param $ttl
     * @param $value
     * @return bool
     */
    public function ct_setex($prefix_mod, $key, $ttl, $value)
    {
        $key = $this->cache_name_handle($prefix_mod, $key);
        $value = $this->cache_save_value($value);

        return $this->setex( $key, $ttl, $value );
    }

    /**
     * 设置key,覆盖
     * @param $prefix_mod
     * @param $key
     * @param $value
     * @return bool
     */
    public function ct_set($prefix_mod, $key, $value)
    {
        $key = $this->cache_name_handle($prefix_mod, $key);
        $value = $this->cache_save_value($value);

        return $this->set( $key, $value );
    }
    /**
     * 设置key,防止覆盖
     * @param $prefix_mod
     * @param $key
     * @param $value
     * @return bool
     */
    public function ct_setnx($prefix_mod, $key, $value)
    {
        $key = $this->cache_name_handle($prefix_mod, $key);
        $value = $this->cache_save_value($value);

        return $this->setnx( $key, $value );
    }

    /**
     * 删除单个或者多个key
     * @param $prefix_mod
     * @param $keys
     */
    public function ct_del( $prefix_mod, $keys )
    {
        if(is_array($keys))
        {
            $new_keys = array();
            foreach($keys as $v)
            {
                $new_keys[] = $this->cache_name_handle($prefix_mod, $v);
            }
        }else{
            $new_keys[] = $keys;
        }

        return $this->del( $new_keys );
    }

    /**
     * 查看key的过期时间
     * @param $prefix_mod
     * @param $key
     * @return int
     */
    public function ct_ttl($prefix_mod, $key)
    {
        $key = $this->cache_name_handle($prefix_mod, $key);
        return $this->ttl($key);
    }


    /**
     * 开启事务
     * @return Redis.
     *
     * * $ret = $redis->multi()
     *      ->set('key1', 'val1')
     *      ->get('key1')
     *      ->set('key2', 'val2')
     *      ->get('key2')
     *      ->exec();
     *
     * //$ret == array (
     * //    0 => TRUE,
     * //    1 => 'val1',
     * //    2 => TRUE,
     * //    3 => 'val2');
     * </pre>
     */
    public function ct_multi()
    {
        return $this->multi();
    }

    /**
     * 执行事务操作
     */
    public function ct_exec()
    {
        $this->exec();
    }

    /**
     * 删除事务
     */
    public function ct_discard()
    {
        $this->discard();
    }

}
